package com.example.activity;

import com.ab.view.listener.AbOnListViewListener;
import com.ab.view.pullview.AbPullListView;
import com.benchi.ncar.R;
import com.example.tools.Constants;
import com.example.tools.IsNetWork;
import com.example.tools.Tools;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class BaseActivity extends Activity {
	protected ProgressDialog mProgressDialog;
	protected AbPullListView mAbPullListView = null;
	protected WebView webview;
	private View mHeadView;

	protected void showWaitingDialog() {
		mProgressDialog = ProgressDialog.show(this, "", "正在加载...", true, true);
	}

	protected void stopWaitingDialog() {
		if (mProgressDialog != null) {
			mProgressDialog.dismiss();
		}
	}

	// Web视图
	protected class HelloWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			if (url.startsWith("mailto:") || url.startsWith("geo:")
					|| url.startsWith("tel:")|| url.startsWith("mqqwpa:")) {
				if (url.startsWith("mqqwpa:")) {
					if (Tools.isAppInstalled(BaseActivity.this, "com.tencent.mobileqq")) {
						Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
						startActivity(intent);
					} else {
						Toast.makeText(BaseActivity.this, "当前手机没有安装QQ",
								Toast.LENGTH_SHORT).show();
						return true;
					}
				} else {
					Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
					startActivity(intent);
				}
				
			}else{
				if (!IsNetWork.isNetworkAvalible(BaseActivity.this)) {
					mAbPullListView.setVisibility(View.VISIBLE);
					webview.setVisibility(View.GONE);
					try {
						mAbPullListView.stopRefresh();
					} catch (Exception e) {
					}
				}else {
					view.loadUrl(url);
				}
			}
			return true;
		}
	}
	
	protected void initViews() {
		mAbPullListView = (AbPullListView) findViewById(R.id.lv_main);
		mAbPullListView.setPullRefreshEnable(true);
		mAbPullListView.setPullLoadEnable(false);
		mHeadView = LayoutInflater.from(this).inflate(
				R.layout.activity_webview, null);
		webview = (WebView) findViewById(R.id.web_main);
		// 设置WebView属性，能够执行Javascript脚本
		webview.getSettings().setJavaScriptEnabled(true);
		mAbPullListView.addHeaderView(mHeadView, null, true);
		mAbPullListView.setAdapter(null);
		mAbPullListView.setAbOnListViewListener(new AbOnListViewListener() {

			@Override
			public void onRefresh() {
				initData();
			}

			@Override
			public void onLoadMore() {
			}

		});
	}

	protected void initData() {
		if (IsNetWork.isNetworkAvalible(this)) {
			mAbPullListView.setVisibility(View.GONE);
			webview.setVisibility(View.VISIBLE);
			// 加载需要显示的网页
//			webview.loadUrl(Constants.USER_URL);
			// 设置Web视图
			webview.setWebViewClient(new HelloWebViewClient());
			webview.setWebChromeClient(new WebChromeClient() {
				public void onProgressChanged(WebView view, int progress) {
					if (progress == 100) {
						if (mProgressDialog != null) {
							mProgressDialog.dismiss();
						}
						try {
							mAbPullListView.stopRefresh();
						} catch (Exception e) {
						}
					}
				}
			});
		} else {
			mAbPullListView.setVisibility(View.VISIBLE);
			webview.setVisibility(View.GONE);
			try {
				mAbPullListView.stopRefresh();
			} catch (Exception e) {
			}
		}

	}
}
